package com.sitech.servlet;

import com.sitech.portal.system.service.ISendPasswordService;
import com.sitech.portal.system.service.impl.SendPasswordServiceImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SendPasswordServlet extends HttpServlet {
    private ISendPasswordService sendPasswordService;

    /**
     * Constructor of the object.
     */
    public SendPasswordServlet() {
        super();
    }

    /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html");
        doPost(request,response);
    }

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            {

        String loginId = (String)request.getParameter("loginId");
        String returnStr="";
        if(loginId==null){
            returnStr="0";
            System.out.println("忘记密码发短信====================="+"loginId==null");
        }else{
            returnStr = sendPasswordService.insertSMSForPassword(loginId);
            System.out.println("忘记密码发短信====================="+returnStr);

        }
                response.setContentType("Content-Type:text/json;charset=UTF-8");
                response.setHeader("Cache-Control", "no-cache");
                PrintWriter out= null;
                try {
                    out = response.getWriter();
                    String callback = request.getParameter("callback");
                    String json = "{\"phone\":\""+returnStr+"\"}";
                    out.write(callback+ "(" + json + ")");
                } catch (IOException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
        out.flush();
        out.close();
    }

    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init(ServletConfig servletConfig){
        try {
            super.init(servletConfig);
        } catch (ServletException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
        sendPasswordService=(SendPasswordServiceImpl) webApplicationContext.getBean("sendPasswordService");
    }

}
